package cn.edu.njnu.opengmpback.Modeling.controller;


import cn.edu.njnu.opengmpback.Modeling.entity.dto.CreateComputeModelDTO;
import cn.edu.njnu.opengmpback.Modeling.service.ComputeModelService;
import cn.edu.njnu.opengmpback.common.entity.FileInfo;
import cn.edu.njnu.opengmpback.common.utils.JsonResult;
import cn.edu.njnu.opengmpback.user.service.UserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping(value = "/computeModel")
public class ComputeModelController {

    @Autowired
    ComputeModelService computeModelService;

    @Autowired
    UserService userService;


    @GetMapping("/test")
    public JsonResult test(){
        JsonResult jsonResult = computeModelService.test();
        return jsonResult;
    }
    @PostMapping
    @ApiOperation(value = "创建计算模型")
    public JsonResult createComputeModel(@RequestBody CreateComputeModelDTO createComputeModelDTO) {
        JsonResult jsonResult = computeModelService.CreateComputeModel(createComputeModelDTO);
        return jsonResult;
    }

    @GetMapping("/open/{modelId}")
    @ApiOperation(value = "打开计算模型")
    public JsonResult OpenProject(@PathVariable(value = "modelId")String modelId){
        JsonResult jsonResult = computeModelService.OpenComputeModel(modelId);
        return jsonResult;
    }

    @GetMapping("/delete/{modelId}")
    @ApiOperation(value = "删除计算模型")
    public JsonResult DeleteProject(@PathVariable(value = "modelId") String modelId){
        JsonResult jsonResult = computeModelService.DeleteComputeModel(modelId);
        return jsonResult;
    }

    @PostMapping("/saveCode/{modelId}")
    @ApiOperation(value = "保存代码")
    public JsonResult saveCode(@PathVariable(value = "modelId")String modelId, @RequestPart("content") String content){
        JsonResult jsonResult = computeModelService.saveCode(modelId, content);
        return jsonResult;
    }

    @PostMapping("/saveWorkFlow/{modelId}")
    @ApiOperation(value = "保存工作流")
    public JsonResult saveWorkFlow(@PathVariable(value = "modelId")String modelId, @RequestBody Map postData){
        JsonResult jsonResult = computeModelService.saveWorkFlow(modelId, postData);
        return jsonResult;
    }

    @PostMapping("/packageOperation/{modelId}")
    @ApiOperation(value = "添加删除依赖")
    public JsonResult packageOperation(@PathVariable(value = "modelId")String modelId, @RequestBody Map postData){
        JsonResult jsonResult = computeModelService.packageOperation(modelId, postData);
        return jsonResult;
    }

    @PostMapping("/removeModelItem/{modelId}")
    @ApiOperation(value = "添加删除依赖")
    public JsonResult removeModelItem(@PathVariable(value = "modelId")String modelId, @RequestBody Map postData){
        JsonResult jsonResult = computeModelService.removeModelItem(modelId, postData);
        return jsonResult;
    }

    @GetMapping("/runDemo/{modelId}")
    @ApiOperation(value = "运行代码")
    public JsonResult runDemo(@PathVariable(value = "modelId")String modelId){
        JsonResult jsonResult = computeModelService.RunDemo(modelId);
        return jsonResult;
    }

    @GetMapping("/runWorkFlow/{modelId}")
    @ApiOperation(value = "运行代码")
    public JsonResult runWorkFlow(@PathVariable(value = "modelId")String modelId){
        JsonResult jsonResult = computeModelService.runWorkFlow(modelId);
        return jsonResult;
    }

    @GetMapping("/stopDemo/{modelId}")
    @ApiOperation(value = "停止运行")
    public JsonResult stopDemo(@PathVariable(value = "modelId")String modelId){
        JsonResult jsonResult = computeModelService.StopDemo(modelId);
        return jsonResult;
    }

    @GetMapping("/stopContainer/{modelId}")
    @ApiOperation(value = "停止容器")
    public JsonResult stopContainer(@PathVariable(value = "modelId")String modelId){
        JsonResult jsonResult = computeModelService.StopContainer(modelId);
        return jsonResult;
    }

    @PostMapping("/getCurDirFiles/{modelId}")
    @ApiOperation(value = "获取当前目录文件")
    List<FileInfo> getCurDirFiles(@PathVariable(value = "modelId")String modelId,@RequestBody Map postData) {
        return computeModelService.getCurDirFiles(modelId, postData);
    }

    @GetMapping("/getModelItems/{modelId}")
    @ApiOperation(value = "获取引入的工具")
    JsonResult getModelItems(@PathVariable(value = "modelId")String modelId) {
        return computeModelService.getModelItems(modelId);
    }

    @PostMapping("/newFolder/{modelId}")
    @ApiOperation(value = "新建文件夹")
    public JsonResult newFolder(@PathVariable(value = "modelId")String modelId, @RequestBody Map postData) {
        String folder = (String) postData.get("folder");
        String path = (String) postData.get("path");
        return  computeModelService.newFolder(modelId, path, folder);
    }

    @PostMapping("/deleteFiles/{modelId}")
    @ApiOperation(value = "删除文件或文件夹")
    public JsonResult deleteFiles(@PathVariable(value = "modelId")String modelId, @RequestBody Map postData) {
       return computeModelService.deleteFiles(modelId, postData);
    }

    @PostMapping("/copyFilesToContainerFormUserSpace/{projectId}")
    @ApiOperation(value = "用户空间传输文件到计算模型")
    public JsonResult copyFilesToContainer(@PathVariable(value = "projectId")String projectId,  @RequestBody Map postData) {
       return computeModelService.copyFilesToContainer(projectId, postData);
    }

    @PostMapping("/copyFilesToContainerFormResCenter/{projectId}")
    @ApiOperation(value = "用户空间传输文件到计算模型")
    public JsonResult copyFilesToContainerFormResCenter(@PathVariable(value = "projectId")String projectId,  @RequestBody Map postData) {
        return computeModelService.copyFilesToContainerFormResCenter(projectId, postData);
    }

    @PostMapping("/copyFilesToLocal/{modelId}")
    @ApiOperation(value = "容器文件传输文件到平台")
    public JsonResult copyFilesToLocal(@PathVariable(value = "modelId")String modelId,  @RequestBody Map postData) {
        return computeModelService.copyFilesToLocal(modelId, postData);
    }

    @PostMapping("/previewFile/{projectId}")
    @ApiOperation(value = "文件预览")
    public JsonResult previewFile(@PathVariable(value = "projectId")String projectId, @RequestBody Map postData) {
        return computeModelService.previewFile(projectId, postData);
    }
}
